//
//  AlbumListController.m
//  Test
//
//  Created by 郭洪军 on 3/29/18.
//  Copyright © 2018 Guo Hongjun. All rights reserved.
//

#import "AlbumListController.h"
#import "LGGetImageAsset.h"
#import "PhotosController.h"
#import "LGHeaderView.h"

@interface AlbumListController ()

@property (nonatomic, strong) NSArray *albumNames;

@end

@implementation AlbumListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"相簿";
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(exit:)];
    
    self.navigationItem.leftBarButtonItem = leftItem;
    NSLog(@"相簿: %@", self.albumNames);
    
}

- (void)exit:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSArray *)albumNames {
    if (!_albumNames) {
        _albumNames = [LGGetImageAsset getAllAlbumsName];
    }
    return _albumNames;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.albumNames.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    
    cell.textLabel.text = self.albumNames[indexPath.row];
//    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld",[LGGetImageAsset getImageArrayWithAlbumName:self.albumNames[indexPath.row]].count];
//    NSLog(@"%@", cell.detailTextLabel.text);
//
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PhotosController *pC = [[PhotosController alloc] init];

    pC.albumName = self.albumNames[indexPath.row];

    [self.navigationController pushViewController:pC animated:YES];
    
//    NSArray<PHAsset *> *assets = [LGGetImageAsset getImageArrayWithAlbumName:self.albumNames[indexPath.row]];
//
//    LGHeaderView *hv = [LGHeaderView headerViewWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height) type:LGHeaderViewShowImage];
//
//    [self.view addSubview:hv];
}

@end
